﻿using Daemon.Data.Infrastructure.Util;
using Daemon.Model;
using Daemon.Repository.Contract;
using Daemon.Service.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Daemon.Service.DaemonProduct
{
	public class DaemonProductService : IDaemonProductService
	{
		private const int CACHE_EXPIRE_DURATION = 1800;

		private readonly IApplicationRepository _applicationRepository;

		private readonly IMemoryCacheUtil _memoryCacheUtil;

		private readonly Dictionary<string, List<string>> _myTFProductNameMapping = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase)
		{
			{ "dvweb", new List<string> { "developer", "developerweb" } },
			{ "tfaweb", new List<string> { "TFAdmin", "Administration" } },
		};

		public DaemonProductService(IApplicationRepository applicationRepository, IMemoryCacheUtil memoryCacheUtil)
		{
			_applicationRepository = applicationRepository;
			_memoryCacheUtil = memoryCacheUtil;
		}

		public Application GetApplicationByProductName(string productName)
		{
			var param = new List<string> { productName };

			var paramToAdd = _myTFProductNameMapping.FirstOrDefault(i => i.Value.Contains(productName.Replace(" ", string.Empty)));

			if (paramToAdd.Key != null)
			{
				param.AddRange(paramToAdd.Value);
			}

			return _applicationRepository.GetApplicationByProductNames(param);
		}

		public List<string> GetTFProductNames(string prefix)
		{
			var indbOne = _applicationRepository.GetApplicationName(prefix);
			var result = new List<string>();
			if (_myTFProductNameMapping.ContainsKey(prefix.ToLower()))
			{
				result = _myTFProductNameMapping[prefix.ToLower()];
			}

			if (!string.IsNullOrEmpty(indbOne))
			{
				result.Add(indbOne);
			}

			return result;
		}
	}
}
